#include "main_Page.h"



//设置阈值中间变量
volatile int16_t SmokeValueTmp = 0;
volatile int8_t TempValueTmp = 0;
volatile int8_t HumiValueTmp = 0;

volatile u8   Key_Set = 0 ; 
volatile u8   Flag_1S = 0 ; 

 void (*cur)(void) = NULL;

volatile uint8_t arr[5]; 


extern volatile uint16_t smoke_upper_val;
extern volatile uint16_t smoke_alarm_count;
extern volatile uint8_t temp_upperValue;
extern volatile uint8_t humi_upperValue;
extern volatile  uint8_t  Smoke_alarm  ; 
extern  uint8_t error_reset  ;
extern  uint8_t card_ID_success     ; 
//记录菜单上一次选择的第几项
volatile uint8_t MainMenu_last_Item = 0;
volatile uint8_t MenuIndex = 0;
volatile uint8_t SetMenu_last_Item = 0;
volatile uint8_t  MainMenuItems = 4 ;
volatile uint8_t  menu_flag = 0 ;



#define MainMenuItems 2
#define SetMenuItems 4
#define PressedTime 10

typedef struct Menu
{
	uint8_t KeyStateIndex;
	uint8_t KeyUpState;
	uint8_t KeyDowmState;
	uint8_t KeyMenuState;
	uint8_t KeyEnterSate;
	void (*CurrentOperate)(void);
}MenuTab;






void System_Init(void)
{ 
    USART1_Init(9600) ; 
	RTC_Config() ;
	OLED_Init() ; 
	USART3_Init(9600)  ;	// 蓝牙初始化
//	W25Q128_Init();
	Security_Init() ; // 舵机初始化和光敏电阻的初始化
	Key_Init() ; 
//	MQ_ADC_Init() ; // MQADC  
//	AT24C02_Init() ; 
//	w25Q128_SectorErase(0x00) ;   // 擦除扇区  
	
//	Tim_6(8400,20000)  ; 
//	TIM_2(8400,20000)  ;
	TIM_7(8400, 10000) ;  // 每1s发生一次中断
//	TIM_ClearFlag(TIM7,TIM_FLAG_Update);
}
void Main_Page_Show(void)
{ 
	char rtc_date_buf[128] = {0};
	char rtc_time_buf[128] = {0} ;
	char str1[50] ; 
	char str2[50] ; 
	char rtc[128] = {0} ; 	
	
	RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
	RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure);
	//获取时间
	sprintf((char*)rtc_time_buf,"%0.2d:%0.2d:%0.2d",RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds);
	//获取当前日期
	sprintf((char*)rtc_date_buf,"%0.2d-%0.2d", RTC_DateStructure.RTC_Month ,  RTC_DateStructure.RTC_Date);
	sprintf(rtc,"%s %s",rtc_date_buf,rtc_time_buf);
	 
	switch(RTC_DateStructure.RTC_WeekDay)
	{
		case 1 :
			strcat((char *)rtc,"Mon");
			break;
		
		case 2 :
			strcat((char *)rtc,"Tue");
			break;
		
		case 3 :
			strcat((char *)rtc,"Wed");
			break;
		
		case 4 :
			strcat((char *)rtc,"Thu");
			break;
		
		case 5 :
			strcat((char *)rtc,"Fri");
			break;
		
		case 6 :
			strcat((char *)rtc,"Sat");
			break;
		
		case 7 :
			strcat((char *)rtc,"Sun");
			break;
	}
//	printf("%s\r\n",rtc) ;
	OLED_show_str(12,0,(uint8_t * )rtc,8);
	

	
	while( 1 == DHT_GetData( arr ) )  ; 
	//在OLED显示温度
	OLED_ShowCHinese(0,6,6,0);
	OLED_ShowCHinese(16,6,7,0);
	sprintf(str1, "%d" , arr[2]) ; 
	OLED_show_str(32,6,(uint8_t*) str1,16);
	
	//在OLED显示湿度
	OLED_ShowCHinese(72,6,8,0);
	OLED_ShowCHinese(88,6,9,0);
	sprintf(str2, "%d" , arr[0]) ;
	OLED_show_str(104,6,(uint8_t * )str2,16);  
	
	
}


uint8_t  Key_scan(void) // 扫描 
{
//	if(Menu_Key   || !Key0_Read || !Key1_Read || !Key2_Read )
//	{
//		Delay_ms(10) ;
//		
//		if(Menu_Key )
//		{
//			return 1  ; 
//		}
//		
//		
//	}
	if(Menu_Key) 
	{
		return 1 ; 
	}
	else if(Key0_Read == 0)
	{
		return 2 ; 
	}
	else if(Key1_Read == 0)
	{
		return 3 ; 
	}
	 if(Key2_Read == 0)
	{
		return 4 ; 
	}
	
	return 0 ; 
}

//系统设置菜单
void Set_Menu(void)
{
	fill_picture(0x00);
	
	if(0 == SetMenu_last_Item)
	{
		show_char(16,0,'>',16);
		show_char(24,0,'>',16);
	}
	else if(1 == SetMenu_last_Item)
	{
		show_char(16,2,'>',16);
		show_char(24,2,'>',16);
	}
	else if(2 == SetMenu_last_Item)
	{
		show_char(16,4,'>',16);
		show_char(24,4,'>',16);
	}
	else if(3 == SetMenu_last_Item)
	{
		show_char(16,6,'>',16);
		show_char(24,6,'>',16);
	}
	
	OLED_ShowCHinese(32,0,10,0);//烟
	OLED_ShowCHinese(48,0,11,0);//雾
	OLED_ShowCHinese(64,0,12,0);//阈
	OLED_ShowCHinese(80,0,13,0);//值
	
	OLED_ShowCHinese(32,2,6,0);//温
	OLED_ShowCHinese(48,2,7,0);//度
	OLED_ShowCHinese(64,2,12,0);//阈
	OLED_ShowCHinese(80,2,13,0);//值
	
	OLED_ShowCHinese(32,4,8,0);//湿
	OLED_ShowCHinese(48,4,9,0);//度
	OLED_ShowCHinese(64,4,12,0);//阈
	OLED_ShowCHinese(80,4,13,0);//值
	
	OLED_ShowCHinese(32,6,4,0);//返
	OLED_ShowCHinese(48,6,5,0);//回
}


void Main_Menu_Enter(void)
{
	if(MainMenu_last_Item == MainMenuItems-1)//返回
	{
		menu_flag = 0;
		fill_picture(0x00);
	}
	
	if(0 == MainMenu_last_Item)//进入系统设置
	{
		fill_picture(0x00);
		MenuIndex = 5;
		SetMenu_last_Item = 0;
		Set_Menu();
	}
}

//主菜单
void Main_menu(void)
{
	menu_flag = 1;
	fill_picture(0x00);
	MenuIndex = 0;
	
	if(0 == MainMenu_last_Item)
	{
		show_char(16,1,'>',16);
		show_char(24,1,'>',16);
	}
	else if(1 == MainMenu_last_Item)
	{
		show_char(16,3,'>',16);
		show_char(24,3,'>',16);
	}
	
	OLED_ShowCHinese(32,1,0,0);//系
	OLED_ShowCHinese(48,1,1,0);//统
	OLED_ShowCHinese(64,1,2,0);//设
	OLED_ShowCHinese(80,1,3,0);//置
	
	OLED_ShowCHinese(32,3,4,0);//返
	OLED_ShowCHinese(48,3,5,0);//回

}

void Main_Menu_Up(void)
{
	//清除原有箭头位置
	show_char(16,1+MainMenu_last_Item*2,' ',16);
	show_char(24,1+MainMenu_last_Item*2,' ',16);
	
	if(0 == MainMenu_last_Item)
	{
		MainMenu_last_Item = MainMenuItems-1;
	}
	else
	{
		MainMenu_last_Item = MainMenu_last_Item -1;
	}
	
	//重新画出新箭头的位置
	show_char(16,1+MainMenu_last_Item*2,'>',16);
	show_char(24,1+MainMenu_last_Item*2,'>',16);
	
}

void Main_Menu_Down(void)
{
	//清除原有箭头位置
	show_char(16,1+MainMenu_last_Item*2,' ',16);
	show_char(24,1+MainMenu_last_Item*2,' ',16);
	
	MainMenu_last_Item = (MainMenu_last_Item+1)%MainMenuItems;
	
	
	//重新画出新箭头的位置
	show_char(16,1+MainMenu_last_Item*2,'>',16);
	show_char(24,1+MainMenu_last_Item*2,'>',16);
}



void Set_Menu_Enter(void)
{
	if(SetMenu_last_Item == SetMenuItems-1)//返回
	{
		
		MainMenu_last_Item = 1;
		Main_menu();
		
	}
	
	if(SetMenu_last_Item == 0)
	{
		MenuIndex = 9;
		SetSmokeMenu();	
	}
	
	if(SetMenu_last_Item == 1)
	{
		MenuIndex = 13;
		SetTempMenu();	
	}
	
	if(SetMenu_last_Item == 2)
	{
		MenuIndex = 17;
		SetHumiMenu();	
	}
}

void Set_Menu_Up(void)
{
	//清除原有箭头位置
	show_char(16,SetMenu_last_Item*2,' ',16);
	show_char(24,SetMenu_last_Item*2,' ',16);
	
	if(0 == SetMenu_last_Item)
	{
		SetMenu_last_Item = SetMenuItems-1;
	}
	else
	{
		SetMenu_last_Item = SetMenu_last_Item -1;
	}
	
	//重新画出新箭头的位置
	show_char(16,SetMenu_last_Item*2,'>',16);
	show_char(24,SetMenu_last_Item*2,'>',16);
}

void Set_Menu_Down(void)
{
	//清除原有箭头位置
	show_char(16,SetMenu_last_Item*2,' ',16);
	show_char(24,SetMenu_last_Item*2,' ',16);
	
	SetMenu_last_Item = (SetMenu_last_Item+1)%SetMenuItems;
		
	//重新画出新箭头的位置
	show_char(16,SetMenu_last_Item*2,'>',16);
	show_char(24,SetMenu_last_Item*2,'>',16);
}

//烟雾阈值设置菜单
void SetSmokeMenu(void)
{
	fill_picture(0x00);
	OLED_ShowCHinese(32,1,10,0);//烟
	OLED_ShowCHinese(48,1,11,0);//雾
	OLED_ShowCHinese(64,1,12,0);//阈
	OLED_ShowCHinese(80,1,13,0);//值
	SmokeValueTmp = smoke_upper_val;
	OLED_ShowNum(48,3,SmokeValueTmp,4,16);
}

void SetSmokeUp(void)
{
	if(SmokeValueTmp < 4096)
	{
		
			SmokeValueTmp += 20;
		
	}

	
	OLED_ShowNum(48,3,SmokeValueTmp,4,16);
}

void SetSmokeDown(void)
{
	if(SmokeValueTmp > 0)
	{

			SmokeValueTmp-=10;
		
		
	}
	
	
	OLED_ShowNum(48,3,SmokeValueTmp,4,16);
}

void SetSmokeEnter(void)
{
	if(12 == MenuIndex)
	{
//		smoke_upperValue = SmokeValueTmp;
//		if(AT24C02_WriteByte(0xA0,0x00,(uint8_t)(SmokeValueTmp&0x00FF)) == 1)
//		{
//			printf("Write Error\n");
//		}
//		if(AT24C02_WriteByte(0xA0,0x01,(uint8_t)((SmokeValueTmp>>8)&0x00FF)) == 1)
//		{
//			printf("Write Error\n");
//		}
//		MenuIndex = 5;
//		Set_Menu();
		
	}
}


//温度阈值设置菜单
void SetTempMenu(void)
{
	fill_picture(0x00);
	OLED_ShowCHinese(32,1,6,0);//温
	OLED_ShowCHinese(48,1,7,0);//度
	OLED_ShowCHinese(64,1,12,0);//阈
	OLED_ShowCHinese(80,1,13,0);//值
	TempValueTmp = temp_upperValue;
	OLED_ShowNum(48,3,TempValueTmp,4,16);
}

void SetTempUp(void)
{
	if(TempValueTmp < 100)
	{

			TempValueTmp++;
		

	}
	

	
	OLED_ShowNum(48,3,TempValueTmp,4,16);
}

void SetTempDown(void)
{

	TempValueTmp--;
	

	
	OLED_ShowNum(48,3,TempValueTmp,4,16);
}

void SetTempEnter(void)
{
//	if(16 == MenuIndex)
//	{
//		temp_upperValue = TempValueTmp;
//		if(AT24C02_WriteByte(0xA0,0x04,TempValueTmp) == 1)
//		{
//			printf("Write Error\n");
//		}
//		MenuIndex = 5;
//		Set_Menu();
//	}
	
}

//湿度阈值设置菜单
void SetHumiMenu(void)
{
	fill_picture(0x00);
	OLED_ShowCHinese(32,1,8,0);//湿
	OLED_ShowCHinese(48,1,9,0);//度
	OLED_ShowCHinese(64,1,12,0);//阈
	OLED_ShowCHinese(80,1,13,0);//值
	HumiValueTmp = humi_upperValue;
	OLED_ShowNum(48,3,HumiValueTmp,4,16);
}

void SetHumiUp(void)
{

	HumiValueTmp++;

	

		
	
	
	OLED_ShowNum(48,3,HumiValueTmp,4,16);
}

void SetHumiDown(void)
{
	
	HumiValueTmp--;
	
	OLED_ShowNum(48,3,HumiValueTmp,4,16);
}

void SetHumiEnter(void)
{
//	if(20 == MenuIndex)
//	{
//		humi_upperValue = HumiValueTmp;
//		if(AT24C02_WriteByte(0xA0,0x03,HumiValueTmp) == 1)
//		{
//			printf("Write Error\n");
//		}
//		MenuIndex = 5;
//		Set_Menu();
//	}
}

void Main_Menu_Menu(void)
{

}

const MenuTab KeyTab[]={
//       |-----------> Index
//       |  Up
//       |  |  Down
//       |  |  |  Menu
//       |  |  |  |  Enter   --->功能函数
//       |  |  |  |  |       |
       { 0, 1, 2, 0, 4,(*Main_menu)},
       { 1, 1, 2, 0, 4,(*Main_Menu_Up)},
       { 2, 1, 2, 0, 4,(*Main_Menu_Down)},
       { 3, 0, 0, 0, 4,(*Main_Menu_Menu)},
       { 4, 0, 0, 0, 4,(*Main_Menu_Enter)},
	   
	   { 5, 6, 7, 0, 8,(*Set_Menu)},
	   { 6, 6, 7, 0, 8,(*Set_Menu_Up)},
	   { 7, 6, 7, 0, 8,(*Set_Menu_Down)},
	   { 8, 0, 0, 0, 8,(*Set_Menu_Enter)},
	   
	   { 9, 10, 11, 5, 12,(*SetSmokeMenu)},
	   { 10, 10, 11, 5, 12,(*SetSmokeUp)},
	   { 11, 10, 11, 5, 12,(*SetSmokeDown)},
	   { 12, 10, 11, 5, 12,(*SetSmokeEnter)},
	   
	   { 13, 14, 15, 5, 16,(*SetTempMenu)},
	   { 14, 14, 15, 5, 16,(*SetTempUp)},
	   { 15, 14, 15, 5, 16,(*SetTempDown)},
	   { 16, 14, 15, 5, 16,(*SetTempEnter)},
	   
	   { 17, 18, 19, 5, 20,(*SetHumiMenu)},
	   { 18, 18, 19, 5, 20,(*SetHumiUp)},
	   { 19, 18, 19, 5, 20,(*SetHumiDown)},
	   { 20, 18, 19, 5, 20,(*SetHumiEnter)},
   };




void main_Page(OS_ERR err)
{
  


		
		if( menu_flag == 0 )
		{
			if(Flag_1S == 1)
			{
			Main_Page_Show() ;
			Flag_1S=0 ;
				
			}
					//温湿度检测程序
			if(0 == card_ID_success && 0 == error_reset && 0 == Security_alarm && Smoke_alarm ==  0 ) 
			{
				temphumi_check();
			}

			
			if(1 == Smoke_alarm)
			{
				// 烟雾警告
				
				OLED_ShowCHinese(32,3,10,0);//烟
				OLED_ShowCHinese(48,3,11,0);//雾
				OLED_ShowCHinese(64,3,24,0);//警
				OLED_ShowCHinese(80,3,25,0);//告
				
				
			}
			
			
			//门锁程序
//			door_card();
			
			//安防程序
//			Security_Start();	
			
	}   
    OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_HMSM_STRICT,&err); //让出CPU，延时1s    
    switch(Key_scan())
    {
        case 1://菜单键     调用菜单
            MenuIndex = KeyTab[MenuIndex].KeyMenuState;
            cur = KeyTab[MenuIndex].CurrentOperate;
            cur();
            break;
        case 2://确定键     调用设置
            if(1 == menu_flag)   // 
            {
                MenuIndex = KeyTab[MenuIndex].KeyEnterSate; // MenuIndex = 4 
                cur = KeyTab[MenuIndex].CurrentOperate;
                cur();
            }		
            break;
        case 3://上 
            if(1 == menu_flag)  // 调用  
            {
                MenuIndex = KeyTab[MenuIndex].KeyUpState; // MenuIndex = 6 
                cur = KeyTab[MenuIndex].CurrentOperate;   //  
                cur();
            }		
            break;
        case 4://下
            if(1 == menu_flag)
            {
                MenuIndex = KeyTab[MenuIndex].KeyDowmState;
                cur = KeyTab[MenuIndex].CurrentOperate;
                cur();
            }			
            break;
        default:
            break;
    }
	OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_HMSM_STRICT,&err); //让出CPU，延时1s


}
//TIM7被初始化为1S定时器
void TIM7_IRQHandler(void)
{

	if(TIM_GetITStatus(TIM7,TIM_IT_Update) == SET)
	{
		Flag_1S = 1;
//		printf("timer 7\r\n") ; 
	
			
	}
	TIM_ClearFlag(TIM7,TIM_FLAG_Update);
	
}
